﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace Defector
{
    /*
     * Mail is the popup window that is called by MainWindow.xaml.cs to display messages
     * from the inbox or trash grids of the messages tab.  Fields are for displaying only,
     * and buttons allow the user to either delete or close the message and window.
     */
    public partial class Mail : Window
    {
        public SqlConnection con;
        DataRow mailSource;
        private int messageId;
        
        

        public Mail(DataRow mailSource)
        {
            /*
             * mailSource is the dataRow associated with the message in the inbox and trash grids.
             * All message data is selected from the database when the MainWindow is opened, or when
             * the messages are refreshed, but not all of the data is displayed in the grid.  This way,
             * when the popup window is opened, all the data is already available, and the connection
             * does not have to be opened to populate this window.
             */
            InitializeComponent();
            setMailSource(mailSource);
        }
 

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        public void setCon(SqlConnection con)
        {
            this.con = con;
        }

        public void setData()
        {
            messageId = Convert.ToInt32(mailSource.ItemArray[0]);
            txtFrom.Text = mailSource.ItemArray[1].ToString();
            txtViewSubject.Text = mailSource.ItemArray[2].ToString();
            txbMessage.Text = mailSource.ItemArray[3].ToString();
            int isNew = Convert.ToInt32(mailSource.ItemArray[4]);
            int isTrash = Convert.ToInt32(mailSource.ItemArray[5]);

            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("UPDATE Messages SET isNew = 0 WHERE (Message_Id = @messageId)", con);
                cmd.Parameters.AddWithValue("@messageId", messageId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Database connection is unstable.  You may experience errors.");
            }
            finally
            {
                con.Close();
            }

            

            if(isTrash == 1)
            {
                btnDelete.IsEnabled = false;
            }
            
        }

        private void setMailSource(DataRow mailSource)
        {
            this.mailSource = mailSource;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("UPDATE Messages SET isTrash = 1 WHERE (Message_Id = @messageId)", con);
                cmd.Parameters.AddWithValue("@messageId", messageId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred while deleting this message.  Please try again.");
            }
            finally
            {
                con.Close();
            }

            this.Close(); ;
        }

    }
}
